<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/6/17
 * Time: 17:52
 */

class myredis{
    public static $instant;
    public function __construct(){

    }

    //强制重新连接redis
    public static function forceReconnect(){
        self::$instant = new  Redis();
        try{
            self::$instant->connect(REDIS_HOST, REDIS_PORT);
            self::$instant->setOption(Redis::OPT_PREFIX, REDIS_PREFIX);//统一前缀
        }catch (Exception $error){//驾驶缓存不生效，则直接连数据库
            /*$arr = ['code'=>-99,'info'=>"缓存服务器异常"];
            j($arr);*/
            self::$instant = false;
            return false;
        }
        return self::$instant;
    }

    public static function getInstant(){
        if(!self::$instant){
            return self::forceReconnect();
        }
        return self::$instant;
    }

    public static function addToSet($arrayKey,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sAdd($arrayKey,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function randPopFromSet($arrayKey){
        return self::popFromSet($arrayKey);
    }

    //随机移除并返回$arrayKey中的一个值
    public static function popFromSet($arrayKey){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->spop($arrayKey);
        }catch (Exception $error){
            var_dump($error);
        }
    }
    public static function getArrayFromSet($arrayKey){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->smembers($arrayKey);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function removeFromSet($arrayKey,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sRemove($arrayKey,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function setArray($key,$obj,$seconds=0){
        if(is_object($obj)|| is_array($obj)){//将对象或者数组序列化
            $value = serialize($obj);
        }

        try{
            return self::set($key,$value,$seconds);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function getArray($key){
        $value = self::get($key);
        $value_serl = @unserialize($value);
        if(is_object($value_serl)||is_array($value_serl)){
            return $value_serl;
        }
        return $value;
    }

    public static function getMultiple($keyArray){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->getMultiple($keyArray);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function get($name){
        $redis = self::getInstant();
        if(!$redis) return false;
        return $redis->get($name);
    }

    public static function set($key,$val,$seconds=0){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            if($seconds){
                return $redis->setex($key,$seconds,$val);
            }else{
                return $redis->set($key,$val);
            }
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function isInArray($arrayName,$val){
        return in_array($val,self::getArray($arrayName));
    }

    //向数组里边添加一个值,
    public static function addToSortArray($arrayName,$val){
        $redis =  self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sAdd($arrayName,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //从数组中删除
    public static function removeFromSortArray($arrayName,$val){
        $redis =  self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sremove($arrayName,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //从数组中删除
    public static function deleteFromSortArray($arrayName,$val){
        $redis =  self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sremove($arrayName,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //获取全部
    public static function getSortArray($arrayName){
        $redis =  self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->sort($arrayName);
        }catch (Exception $error){
            var_dump($error);
        }
    }


    //向key里边添加一个值,
    public static function sAdd($key,$val){
        $redis =  self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sAdd($key,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //从key里边删除一个值
    public static function sRemove($key,$val){
        $redis =  self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sremove($key,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function delete($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->delete($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function lget($name,$pos){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lget($name,$pos);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function lRemoveByVal($name,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sRemove($name,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }
    public static function sRemoveByVal($name,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->sRemove($name,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function getAllList($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lgetrange($name, 0,-1);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function lpush($name,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lPush($name,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function getValByIndex($name,$index){
        $redis = self::getInstant();
        if(!$redis) return false;
        return $redis->get("myVal{$index}_".$name);
    }

    public static function getTailIndex($name){
        $redis = self::getInstant();
        if(!$redis) return false;
        return $redis->get("tailIndex_".$name);//链表的尾巴
    }

    public static function mypop($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            $headIndex = $redis->get("headIndex_".$name);//链表的头部
            $tailIndex = $redis->get("tailIndex_".$name);//链表的尾巴
            if($tailIndex>=($headIndex+1)){
                $redis->incr("headIndex_".$name);
                $pos = $headIndex+1;
                $val  = $redis->get("myVal{$pos}_".$name);
                return $val;
            }else {
                //$redis->set("tailIndex_" . $name, $headIndex);//链表的尾巴
                return false;
            }
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //放入
    public static function mypush($name,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            $head = $redis->get("headIndex_".$name);//链表的头部
            if(!$head) {//链表的头部
                $redis->del("headIndex_".$name);
                $redis->set("headIndex_".$name,0);
            }

            $tailIndex = $redis->get("tailIndex_".$name);//链表的尾巴
            if(!$tailIndex) {//链表的尾巴
                $redis->del("tailIndex_".$name);
                $redis->set("tailIndex_".$name,0);
            }
            $newTailIndex = $redis->incr("tailIndex_".$name);
            return $redis->set("myVal{$newTailIndex}_".$name,$val,3600*24*30);//一个月有效期
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function rpush($name,$val){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->rPush($name,$val);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function lpop($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lPop($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function rpop($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->rPop($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function llen($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->llen($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function lsize($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lsize($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function size($name){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->lsize($name);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static function increment($id, $offset = 1)
    {
        $redis = self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->incr($id, $offset);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    public static  function decrement($id, $offset = 1)
    {
        $redis = self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->decr($id, $offset);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //返回部分
    public static function getRange($id,$start,$end){
        $redis = self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->lgetrange($id, $start,$end);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //删除指定值
    public static function deleteByVal($id,$val){
        $redis = self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->sremove($id, $val);
        }catch (Exception $error){
            var_dump($error);
        }
    }



    //有序集合，注意，这里，不可以将所有人的分数都写入，只能先读取排行榜，如果新人可以进榜单，才写入，并移除最后的名次,1000名吧
    public static  function zAdd($keySet,$val,$who){//如果每次$who不一样，则这个会越加越多哇
        $redis = self::getInstant();
        if(!$redis) return false;
        try{
            return $redis->zAdd($keySet,$val,$who);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //删除一个
    public static  function zDelete($keySet,$who){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zDelete($keySet,$who);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //获取前多少名
    public static  function zRange($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //获取前多少名
    public static  function range($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //倒排序
    public static  function rRange($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRevRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }
    //倒排序
    public static  function rangeR($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRevRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //倒排序
    public static  function zRRange($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRevRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }

    //倒排序
    public static  function zRevRange($keySet,$start, $end,$withScores=true){
        $redis = self::getInstant();
        if(!$redis) return false;

        try{
            return $redis->zRevRange($keySet,$start, $end,$withScores);
        }catch (Exception $error){
            var_dump($error);
        }
    }

}